package com.hckjsec.cloud.controller;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.hckjsec.cloud.domain.*;
import com.hckjsec.cloud.domain.view.*;
import com.hckjsec.cloud.enums.BasicParamEnum;
import com.hckjsec.cloud.enums.publicParamEnum;
import com.hckjsec.cloud.service.*;
import com.hckjsec.common.init.YwInitData;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.common.utils.EnumUtil;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import com.hckjsec.common.utils.uuid.IdWorker;
import com.hckjsec.system.domain.SysMatter;
import com.hckjsec.system.service.ISysMatterService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * applets配置Controller
 * 
 * @author cyt
 * @date 2021-07-27
 */
@RestController
@RequestMapping("/system/appconfig")
public class AppletsConfigController extends BaseController
{
    @Autowired
    private IAppletsConfigService appletsConfigService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private YwInitData ywInitData;

    @Autowired
    private IAppletsConfigParamService appletsConfigParamService;

    @Autowired
    private IAppletsConfigParamDetailService appletsConfigParamDetailService;

    @Autowired
    private ISysMatterService sysMatterService;

    @Autowired
    private IAppletsConfigMatterService appletsConfigMatterService;

    @Autowired
    private IAppletsConfigMatterDetailService appletsConfigMatterDetailService;

    @Autowired
    private IAppletsPublicParamService appletsPublicParamService;

    @Autowired
    private IAppletsPublicMechanismService appletsPublicMechanismService;

    @Autowired
    private IAppletsPublicMechanismDetailService appletsPublicMechanismDetailService;
    /**
     * 查询applets配置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppletsConfig appletsConfig)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            appletsConfig.setDeptId(SecurityUtils.getDeptId());
        }
        List<AppletsConfig> list = appletsConfigService.selectAppletsConfigList(appletsConfig);
        return getDataTable(list);
    }

    /**
     * 取所有机构的id和名称
     */
    @GetMapping("/configlist")
    public AjaxResult configlistlist()
    {
        List<AppletsConfig> list = appletsConfigService.selectAppletsConfigList(null);
        return AjaxResult.success(list);
    }

    /**
     * 导出applets配置列表
     */
    @Log(title = "applets配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppletsConfig appletsConfig)
    {
        List<AppletsConfig> list = appletsConfigService.selectAppletsConfigList(appletsConfig);
        ExcelUtil<AppletsConfig> util = new ExcelUtil<AppletsConfig>(AppletsConfig.class);
        return util.exportExcel(list, "config");
    }

    /**
     * 获取applets配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(appletsConfigService.selectAppletsConfigById(id));
    }

    /**
     * 新增applets配置
     */
    @Log(title = "applets配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppletsConfig appletsConfig)
    {
        appletsConfig.setType(1L);
        appletsConfig.setOperator(SecurityUtils.getUserId());
        int rows=appletsConfigService.insertAppletsConfig(appletsConfig);
        if(rows > 0){//初始化
            appletsConfigService.AppletsConfigAppidInit(appletsConfig.getAppid(),appletsConfig.getId());
            appletsConfigService.AppletsConfigInit(appletsConfig.getId(),appletsConfig);
        }
        return toAjax(rows);
    }

    /**
     * 修改applets配置
     */
    @Log(title = "applets配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppletsConfig appletsConfig)
    {
        int rows=appletsConfigService.updateAppletsConfig(appletsConfig);
        if(rows > 0){//初始化
            appletsConfigService.AppletsConfigAppidInit(appletsConfig.getAppid(),appletsConfig.getId());
            appletsConfigService.AppletsConfigInit(appletsConfig.getId(),appletsConfig);
        }
        return toAjax(rows);
    }

    /**
     * 删除applets配置
     */
    @Log(title = "applets配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appletsConfigService.deleteAppletsConfigByIds(ids));
    }


    /**
     * 获取applets配置应用
     */
    @GetMapping(value = "/configure/{id}")
    public AjaxResult getConfigureList(@PathVariable("id") Long id)
    {
        BasicAppletsConfig basicAppletsConfig = new BasicAppletsConfig();
        basicAppletsConfig.setId(id);
        basicAppletsConfig.setStaticurl("https://hf.hckjsec.com");
        List<ConfigBasicParam> configList=appletsConfigParamService.configBasicParamList(id);
        basicAppletsConfig.setConfigList(configList);
        BasicView basicView = appletsConfigParamDetailService.getBasicView(id);
        basicAppletsConfig.setBasicView(basicView);
        return AjaxResult.success(basicAppletsConfig);
    }



    /**
     * 修改参数状态
     */
    @PutMapping(value = "/saveconfigure")
    public AjaxResult saveConfigure(@RequestBody ConfigBasicParam configBasicParam)
    {
        AppletsConfigParam appletsConfigParam = new AppletsConfigParam();
        appletsConfigParam.setConfigId(configBasicParam.getId());
        appletsConfigParam.setParamnumber(configBasicParam.getConfigname());
        appletsConfigParam.setState(configBasicParam.getEnable());
        int row=appletsConfigParamService.updateAppletsConfigParamByConfigId(appletsConfigParam);
        if (row > 0){
            List<Map<String,String>> stateMap= appletsConfigParamService.findMap(configBasicParam.getId());
            ywInitData.modIdByConfigState(configBasicParam.getId(),ComUtil.mapListByMap(stateMap,"paramnumber"));
        }
        return AjaxResult.success();
    }


    /**
     * 添加参数明细
     */
    @PutMapping(value = "/saveconfiguredetail")
    public AjaxResult saveConfigureDetail(@RequestBody BasicView basicView)
    {
        Long configId=basicView.getId();
        Long userId=SecurityUtils.getUserId();
        if (!StringUtils.isNull(basicView)){
            //告知书
            KvView inform = basicView.getInform();
            if (!StringUtils.isNull(inform)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_INFORM.getKey());
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber(BasicParamEnum.SYS_APPLETS_BASIC_INFORM.getKey());
                if (!"".equals(StringUtils.isHcNull(inform.getValue(),""))){//解密
                    appletsConfigParamDetail.setContent(Base64Converter.decode(inform.getValue()));
                }else{
                    appletsConfigParamDetail.setContent(inform.getValue());
                }
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
            //百度Ocr
            BdocrView bdocr = basicView.getBdocr();
            if (!StringUtils.isNull(bdocr)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_BDOCR.getKey());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("appid");
                appletsConfigParamDetail.setContent(bdocr.getAppid());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("appkey");
                appletsConfigParamDetail.setContent(bdocr.getAppkey());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("appsecret");
                appletsConfigParamDetail.setContent(bdocr.getAppsecret());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
            //轮播图
            List<KvView> rotation = basicView.getRotation();
            if (!StringUtils.isNull(rotation)) {
                if (rotation.size() > 0) {
                    AppletsConfigParamDetail appletsConfigParamDetail = new AppletsConfigParamDetail(configId, BasicParamEnum.SYS_APPLETS_BASIC_ROTATION.getKey());
                    appletsConfigParamDetail.setOperator(userId);
                    appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                    rotation.forEach(kv -> {
                        appletsConfigParamDetail.setParamdetailnumber(BasicParamEnum.SYS_APPLETS_BASIC_ROTATION.getKey());
                        appletsConfigParamDetail.setName(kv.getName());
                        appletsConfigParamDetail.setContent(kv.getUrl());
                        appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                    });
                }
            }
            //小程序模板
            TemplateView template = basicView.getTemplate();
            if (!StringUtils.isNull(template)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_TEMPLATE.getKey());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("bidTemplate");
                appletsConfigParamDetail.setContent(template.getBidTemplate());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("videoTemplate");
                appletsConfigParamDetail.setContent(template.getVideoTemplate());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("materialsTemplate");
                appletsConfigParamDetail.setContent(template.getMaterialsTemplate());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
            //远程视频配置
            RemotevideoView remotevideo = basicView.getRemotevideo();
            if (!StringUtils.isNull(remotevideo)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_REMOTEVIDEO.getKey());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("appid");
                appletsConfigParamDetail.setContent(remotevideo.getAppid());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("key");
                appletsConfigParamDetail.setContent(remotevideo.getKey());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("secretId");
                appletsConfigParamDetail.setContent(remotevideo.getSecretId());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("secretKey");
                appletsConfigParamDetail.setContent(remotevideo.getSecretKey());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
            //短信参数配置
            MessageView message = basicView.getMessage();
            if (!StringUtils.isNull(message)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_MESSAGE.getKey());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("secretId");
                appletsConfigParamDetail.setContent(message.getSecretId());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("secretKey");
                appletsConfigParamDetail.setContent(message.getSecretKey());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("sdkAppid");
                appletsConfigParamDetail.setContent(message.getSdkAppid());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("sign");
                appletsConfigParamDetail.setContent(message.getSign());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
            //菜单配置配置
            MenuView menu = basicView.getMenu();
            if (!StringUtils.isNull(menu)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_MENU.getKey());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("appointmentBtn");
                appletsConfigParamDetail.setContent(menu.getAppointmentBtn());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("videoBtn");
                appletsConfigParamDetail.setContent(menu.getVideoBtn());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("guideBtn");
                appletsConfigParamDetail.setContent(menu.getGuideBtn());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("chargeBtn");
                appletsConfigParamDetail.setContent(menu.getChargeBtn());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("verificationBtn");
                appletsConfigParamDetail.setContent(menu.getVerificationBtn());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("teamBtn");
                appletsConfigParamDetail.setContent(menu.getTeamBtn());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("newsBtn");
                appletsConfigParamDetail.setContent(menu.getNewsBtn());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("noticeBtn");
                appletsConfigParamDetail.setContent(menu.getNoticeBtn());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
            //活体检测配置
            CheckView livingthing = basicView.getLivingthing();
            if (!StringUtils.isNull(livingthing)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_LIVINGTHING.getKey());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("secretId");
                appletsConfigParamDetail.setContent(livingthing.getSecretId());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("ruleId");
                appletsConfigParamDetail.setContent(livingthing.getRuleId());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("region");
                appletsConfigParamDetail.setContent(livingthing.getRegion());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("secretKey");
                appletsConfigParamDetail.setContent(livingthing.getSecretKey());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
            //身份核验配置
            CheckView identity = basicView.getIdentity();
            if (!StringUtils.isNull(identity)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_IDENTITY.getKey());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("secretId");
                appletsConfigParamDetail.setContent(identity.getSecretId());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("ruleId");
                appletsConfigParamDetail.setContent(identity.getRuleId());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("region");
                appletsConfigParamDetail.setContent(identity.getRegion());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("secretKey");
                appletsConfigParamDetail.setContent(identity.getSecretKey());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
            //契约锁配置
            QysView qys = basicView.getQys();
            if (!StringUtils.isNull(qys)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_QYS.getKey());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("name");
                appletsConfigParamDetail.setContent(qys.getName());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("url");
                appletsConfigParamDetail.setContent(qys.getUrl());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("accesskey");
                appletsConfigParamDetail.setContent(qys.getAccesskey());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("accessSecret");
                appletsConfigParamDetail.setContent(qys.getAccessSecret());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("callbackDecryptKey");
                appletsConfigParamDetail.setContent(qys.getCallbackDecryptKey());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber("environment");
                appletsConfigParamDetail.setContent(qys.getEnvironment());
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
            //指引收费标准
            List<KvView> guide = basicView.getGuide();
            if (!StringUtils.isNull(guide)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_GUIDE.getKey());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                guide.forEach(kv -> {
                    appletsConfigParamDetail.setParamdetailnumber(BasicParamEnum.SYS_APPLETS_BASIC_GUIDE.getKey());
                    appletsConfigParamDetail.setName(kv.getName());
                    appletsConfigParamDetail.setContent(kv.getUrl());
                    appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
                });
            }

            //付款码
            KvView paycode = basicView.getPayment();
            if (!StringUtils.isNull(paycode)){
                AppletsConfigParamDetail appletsConfigParamDetail=new AppletsConfigParamDetail(configId,BasicParamEnum.SYS_APPLETS_BASIC_PAYMENT.getKey());
                appletsConfigParamDetailService.deleteAppletsConfigParamDetailByConfigId(appletsConfigParamDetail);
                appletsConfigParamDetail.setParamdetailnumber(BasicParamEnum.SYS_APPLETS_BASIC_PAYMENT.getKey());
                appletsConfigParamDetail.setName(paycode.getName());
                appletsConfigParamDetail.setContent(paycode.getUrl());
                appletsConfigParamDetail.setOperator(userId);
                appletsConfigParamDetailService.insertAppletsConfigParamDetail(appletsConfigParamDetail);
            }
           //  ywInitData.modIdByBasicViewMap(configId,basicView);
        }
        //初始化
        BasicView basicViewInit =appletsConfigParamDetailService.getBasicView(configId);
        appletsConfigParamDetailService.IdByBasicViewInit(configId,basicViewInit);
        return AjaxResult.success();
    }


    /**
     * 获取applets配置事项
     */
    @GetMapping(value = "/matter/{id}")
    public AjaxResult getMatter(@PathVariable("id") Long id)
    {
        List<ConfigMatterParam> configMatterParamList =  new ArrayList<ConfigMatterParam>();
        Long userId=SecurityUtils.getUserId();
        List<Map<String,String>> stateMap= appletsConfigMatterService.findMap(id);
        Map<String,String> resState = ComUtil.mapListByMap(stateMap,"matter_number");
        SysMatter sysMatter = new SysMatter();
        sysMatter.setIschildren("Y");
        List<SysMatter> SysMatterList= sysMatterService.selectSysMatterList(sysMatter);
        SysMatterList.forEach(matter -> {
            ConfigMatterParam configMatterParam = new ConfigMatterParam();
            configMatterParam.setId(id);
            configMatterParam.setMatterNumber(matter.getMatterNumber());
            configMatterParam.setMatterName(matter.getMatterName());
            if(resState.get(matter.getMatterNumber()) == null){
                AppletsConfigMatter appletsConfigMatter = new AppletsConfigMatter();
                appletsConfigMatter.setConfigId(id);
                appletsConfigMatter.setMatterNumber(matter.getMatterNumber());
                appletsConfigMatter.setState("false");
                appletsConfigMatter.setOperator(userId);
                appletsConfigMatterService.insertAppletsConfigMatter(appletsConfigMatter);
                configMatterParam.setEnable("false");
            }else{
                configMatterParam.setEnable(resState.get(matter.getMatterNumber()));
            }
            configMatterParamList.add(configMatterParam);
        });
        return AjaxResult.success(configMatterParamList);
    }


    /**
     * 修改事项参数状态
     */
    @PutMapping(value = "/savematterconfigure")
    public AjaxResult saveMatterConfigure(@RequestBody ConfigMatterParam configMatterParam)
    {
        AppletsConfigMatter appletsConfigMatter = new AppletsConfigMatter();
        appletsConfigMatter.setConfigId(configMatterParam.getId());
        appletsConfigMatter.setMatterNumber(configMatterParam.getMatterNumber());
        appletsConfigMatter.setState(configMatterParam.getEnable());
        appletsConfigMatterService.updateAppletsConfigByConfigId(appletsConfigMatter);
        return AjaxResult.success();
    }

    /**
     * 添加参数明细
     */
    @PutMapping(value = "/savematterconfiguredetail")
    public AjaxResult savematterconfiguredetail(@RequestBody MatterConfig matterConfig)
    {
        Long configId = matterConfig.getId();
        String matterNumber = matterConfig.getMatterNumber();
        Long userId = SecurityUtils.getUserId();
        List<CategoryView> category=matterConfig.getCategory();
        if (!StringUtils.isNull(category)){
            AppletsConfigMatterDetail appletsConfigMatterDetail = new AppletsConfigMatterDetail(configId,matterNumber,"0");
            appletsConfigMatterDetail.setOperator(userId);
            appletsConfigMatterDetailService.deleteAppletsConfigParamDetailByNumberId(appletsConfigMatterDetail);
            category.forEach(kv -> {
                appletsConfigMatterDetail.setMatterType("0");
                appletsConfigMatterDetail.setTypeNumber(kv.getCategoryNumber());
                appletsConfigMatterDetail.setState(kv.getEnable());
                appletsConfigMatterDetailService.insertAppletsConfigMatterDetail(appletsConfigMatterDetail);
            });
        }
        List<MateriaView> materia=matterConfig.getMateria();
        if (!StringUtils.isNull(materia)){
            AppletsConfigMatterDetail appletsConfigMatterDetail = new AppletsConfigMatterDetail(configId,matterNumber,"1");
            appletsConfigMatterDetail.setOperator(userId);
            appletsConfigMatterDetailService.deleteAppletsConfigParamDetailByNumberId(appletsConfigMatterDetail);
            materia.forEach(kv -> {
                appletsConfigMatterDetail.setMatterType("1");
                appletsConfigMatterDetail.setTypeNumber(kv.getMateriaNumber());
                appletsConfigMatterDetail.setMust(kv.getIsmust());
                appletsConfigMatterDetailService.insertAppletsConfigMatterDetail(appletsConfigMatterDetail);
            });
        }
        List<CostView> cost =matterConfig.getCost();
        if (!StringUtils.isNull(cost)){
            AppletsConfigMatterDetail appletsConfigMatterDetail = new AppletsConfigMatterDetail(configId,matterNumber,"2");
            appletsConfigMatterDetail.setOperator(userId);
            appletsConfigMatterDetailService.deleteAppletsConfigParamDetailByNumberId(appletsConfigMatterDetail);
            cost.forEach(kv -> {
                appletsConfigMatterDetail.setMatterType("2");
                appletsConfigMatterDetail.setTypeNumber(kv.getCostNumber());
                appletsConfigMatterDetail.setMoney(kv.getMoney());
                appletsConfigMatterDetailService.insertAppletsConfigMatterDetail(appletsConfigMatterDetail);
            });
        }
        return AjaxResult.success();
    }

    /**
     * 添加参数明细
     */
    @PostMapping(value = "/getmatterconfiguredetail")
    public AjaxResult getmatterconfiguredetail(@RequestBody  ConfigMatterParam configMatterParam)
    {
        MatterConfig matterConfig= new MatterConfig();
        Long configId = configMatterParam.getId();
        matterConfig.setId(configMatterParam.getId());
        String matterNumber = configMatterParam.getMatterNumber();
        matterConfig.setMatterNumber(matterNumber);
        Long userId = SecurityUtils.getUserId();
        AppletsConfigMatterDetail appletsConfigMatterDetail = new AppletsConfigMatterDetail(configId,matterNumber);
        List<AppletsConfigMatterDetail> appletsConfigMatterDetailList=appletsConfigMatterDetailService.selectAppletsConfigMatterDetailList(appletsConfigMatterDetail);
        List<CategoryView> category= new ArrayList<CategoryView>();
        List<MateriaView> materia = new ArrayList<MateriaView>();
        List<CostView> cost = new ArrayList<CostView>();
        appletsConfigMatterDetailList.forEach(kv -> {
            if ("0".equals(kv.getMatterType())){
                CategoryView categoryView= new CategoryView();
                categoryView.setCategoryNumber(kv.getTypeNumber());
                categoryView.setEnable(kv.getState());
                category.add(categoryView);
            }else if("1".equals(kv.getMatterType())){
                MateriaView materiaView = new MateriaView();
                materiaView.setMateriaNumber(kv.getTypeNumber());
                materiaView.setIsmust(kv.getMust());
                materia.add(materiaView);
            }else if("2".equals(kv.getMatterType())){
                CostView costView = new CostView();
                costView.setCostNumber(kv.getTypeNumber());
                costView.setMoney(kv.getMoney());
                cost.add(costView);
            }
        });
        matterConfig.setCategory(category);
        matterConfig.setMateria(materia);
        matterConfig.setCost(cost);
        return AjaxResult.success(matterConfig);
    }



    /**
     * 复制事项编号
     */
    @PostMapping(value = "/modcopymatter")
    public AjaxResult modcopymatter(@RequestBody KvView kvView)
    {
          String oldId=kvView.getKey();
          String newId=kvView.getValue();
          appletsConfigMatterService.deleteAppletsConfigMatterByConfigId(Long.valueOf(newId));
          AppletsConfigMatter appletsConfigMatter = new AppletsConfigMatter();
          appletsConfigMatter.setConfigId(Long.valueOf(oldId));
          List<AppletsConfigMatter> matterList = appletsConfigMatterService.selectAppletsConfigMatterList(appletsConfigMatter);
          matterList.forEach(kv->{
              kv.setConfigId(Long.valueOf(newId));
              appletsConfigMatterService.insertAppletsConfigMatter(kv);
          });
          appletsConfigMatterDetailService.deleteAppletsConfigMatterDetailByConfig(Long.valueOf(newId));
          AppletsConfigMatterDetail appletsConfigMatterDetail = new AppletsConfigMatterDetail();
          appletsConfigMatterDetail.setConfigId(Long.valueOf(oldId));
          List<AppletsConfigMatterDetail> appletsConfigMatterDetailList = appletsConfigMatterDetailService.selectAppletsConfigMatterDetailList(appletsConfigMatterDetail);
          appletsConfigMatterDetailList.forEach(kv ->{
              kv.setConfigId(Long.valueOf(newId));
              appletsConfigMatterDetailService.insertAppletsConfigMatterDetail(kv);
          });
          return AjaxResult.success();
    }



    /**
     * 小程序用户参数配置表保存
     */
    @PostMapping(value = "/publicParamSave")
    public AjaxResult modcopymatter(@RequestBody AppletsPublicParamView appletsPublicParamView)
    {
        //指定公证员短信
        String notificationPhone=appletsPublicParamView.getNotificationPhone();
        List<AppletsPublicParam> specifyData=appletsPublicParamView.getSpecifyData();
        if (!StringUtils.isNull(notificationPhone)){
            AppletsPublicParam appletsPublicParam=new AppletsPublicParam();
            appletsPublicParam.setDeptId(SecurityUtils.getDeptId());
            appletsPublicParam.setParamType(publicParamEnum.APPLETS_PUBLIC_PARAM_NOTIFICATIONPHONE.getKey());
            appletsPublicParamService.deleteAppletsPublicParamByParamType(appletsPublicParam);
            if (!"".equals(StringUtils.isHcNull(notificationPhone,""))){
                appletsPublicParam.setPhone(notificationPhone);
                appletsPublicParam.setOperator(SecurityUtils.getUserId());
                appletsPublicParamService.insertAppletsPublicParam(appletsPublicParam);
            }
        }
        if (!StringUtils.isNull(specifyData)){
            AppletsPublicParam appletsPublicParam=new AppletsPublicParam();
            appletsPublicParam.setDeptId(SecurityUtils.getDeptId());
            appletsPublicParam.setParamType(publicParamEnum.APPLETS_PUBLIC_PARAM_SPECIFY.getKey());
            appletsPublicParamService.deleteAppletsPublicParamByParamType(appletsPublicParam);
            if(specifyData.size()>0) {
                specifyData.forEach(kv -> {
                    String category="";
                    if (kv.getCategorya().length>0){
                        category = StringUtils.join(kv.getCategorya(), ",");
                    }
                    appletsPublicParam.setCategory(category);
                    appletsPublicParam.setUrl(kv.getUrl());
                    appletsPublicParam.setUsername(kv.getUsername());
                    appletsPublicParam.setPhone(kv.getPhone());
                    appletsPublicParam.setSort(kv.getSort());
                    if ("".equals(StringUtils.isHcNull(kv.getState(),""))){
                        appletsPublicParam.setState("false");
                    }else{
                        appletsPublicParam.setState(kv.getState());
                    }
                    appletsPublicParam.setOperator(SecurityUtils.getUserId());
                    appletsPublicParamService.insertAppletsPublicParam(appletsPublicParam);
                });
            }
        }
        return AjaxResult.success();
    }

    /**
     * 小程序用户参数配置表获取
     */
    @GetMapping(value = "/getPublicParam")
    public AjaxResult getPublicParam()
    {
        AppletsPublicParamView appletsPublicParamView = new AppletsPublicParamView();
        AppletsPublicParam notificationPhone=new AppletsPublicParam();
        notificationPhone.setDeptId(SecurityUtils.getDeptId());
        notificationPhone.setParamType(publicParamEnum.APPLETS_PUBLIC_PARAM_NOTIFICATIONPHONE.getKey());
        List<AppletsPublicParam> notificationPhoneList=appletsPublicParamService.selectAppletsPublicParamList(notificationPhone);
        if (!StringUtils.isNull(notificationPhoneList) && notificationPhoneList.size()>0){
            appletsPublicParamView.setNotificationPhone(notificationPhoneList.get(0).getPhone());
        }
        AppletsPublicParam specifyData=new AppletsPublicParam();
        specifyData.setDeptId(SecurityUtils.getDeptId());
        specifyData.setParamType(publicParamEnum.APPLETS_PUBLIC_PARAM_SPECIFY.getKey());
        List<AppletsPublicParam> specifyDataList=appletsPublicParamService.selectAppletsPublicParamList(specifyData);
        if (!StringUtils.isNull(specifyDataList) && specifyDataList.size()>0){
            specifyDataList.forEach(kv->{
                String category = StringUtils.isHcNull(kv.getCategory(),"");
                if (!"".equals(category)){
                    kv.setCategorya(category.split(","));
                }
            });
            appletsPublicParamView.setSpecifyData(specifyDataList);
        }
        return AjaxResult.success(appletsPublicParamView);
    }


    /**
     * 小程序用户参数机构简介保存
     */
    @PostMapping(value = "/publicMechanismSave")
    public AjaxResult publicMechanismSave(@RequestBody AppletsPublicMechanismView appletsPublicMechanismView)
    {
        Long mechanismId=0L;
        AppletsPublicMechanism appletsPublicMechanism = new AppletsPublicMechanism(SecurityUtils.getDeptId());
        List<AppletsPublicMechanism> appletsPublicMechanismList = appletsPublicMechanismService.selectAppletsPublicMechanismList(appletsPublicMechanism);
        if (!StringUtils.isEmpty(appletsPublicMechanismList)){
            mechanismId=appletsPublicMechanismList.get(0).getMechanismId();
            AppletsPublicMechanism  publicMechanism = appletsPublicMechanismService.selectAppletsPublicMechanismById(mechanismId);
            publicMechanism.setMechanismImg(appletsPublicMechanismView.getMechanismImg());
            if (!"".equals(StringUtils.isHcNull(appletsPublicMechanismView.getContent(),""))){//解密
                publicMechanism.setContent(Base64Converter.decode(appletsPublicMechanismView.getContent()));
            }else{
                publicMechanism.setContent(appletsPublicMechanismView.getContent());
            }
            appletsPublicMechanismService.updateAppletsPublicMechanism(publicMechanism);
        }else{
            AppletsPublicMechanism publicMechanism = new AppletsPublicMechanism();
            publicMechanism.setDeptId(SecurityUtils.getDeptId());
            publicMechanism.setMechanismImg(appletsPublicMechanismView.getMechanismImg());
            if (!"".equals(StringUtils.isHcNull(appletsPublicMechanismView.getContent(),""))){//解密
                publicMechanism.setContent(Base64Converter.decode(appletsPublicMechanismView.getContent()));
            }else{
                publicMechanism.setContent(appletsPublicMechanismView.getContent());
            }
            publicMechanism.setOperator(SecurityUtils.getUserId());
            appletsPublicMechanismService.insertAppletsPublicMechanism(publicMechanism);
            mechanismId=publicMechanism.getMechanismId();
        }
        appletsPublicMechanismDetailService.deleteAppletsPublicMechanismDetailById(mechanismId);
        List<AppletsPublicMechanismDetail> appletsPublicMechanismDetails=appletsPublicMechanismView.getAppletsPublicMechanismDetails();
        Long finalMechanismId = mechanismId;
        appletsPublicMechanismDetails.forEach(kv->{
              kv.setMechanismId(finalMechanismId);
              kv.setDeptId(SecurityUtils.getDeptId());
              appletsPublicMechanismDetailService.insertAppletsPublicMechanismDetail(kv);
        });
        return AjaxResult.success();
    }


    /**
     * 小程序用户参数机构简介保存
     */
    @GetMapping(value = "/getMechanism")
    public AjaxResult getMechanismSave()
    {
        AppletsPublicMechanismView appletsPublicMechanismView = new AppletsPublicMechanismView();
        AppletsPublicMechanism appletsPublicMechanism = new AppletsPublicMechanism(SecurityUtils.getDeptId());
        List<AppletsPublicMechanism> appletsPublicMechanismList = appletsPublicMechanismService.selectAppletsPublicMechanismList(appletsPublicMechanism);
        if (!StringUtils.isEmpty(appletsPublicMechanismList)){
            AppletsPublicMechanism newAppletsPublicMechanism = appletsPublicMechanismList.get(0);
            appletsPublicMechanismView.setMechanismImg(newAppletsPublicMechanism.getMechanismImg());
            appletsPublicMechanismView.setContent(newAppletsPublicMechanism.getContent());
            List<AppletsPublicMechanismDetail> appletsPublicMechanismDetails = appletsPublicMechanismDetailService.selectAppletsPublicMechanismDetailList(new AppletsPublicMechanismDetail(newAppletsPublicMechanism.getMechanismId()));
            appletsPublicMechanismView.setAppletsPublicMechanismDetails(appletsPublicMechanismDetails);
        }
        return AjaxResult.success(appletsPublicMechanismView);
    }
}